package com.ai.module.user.service.impl;

import com.ai.module.chat.common.base.ThreadLocalContext;
import com.ai.module.chat.common.constant.FileStringConstant;
import com.ai.module.chat.common.domain.vo.AliFileVo;
import com.ai.module.chat.config.properties.AliyunProperty;
import com.ai.module.user.domain.entity.UserImageEntity;
import com.ai.module.user.domain.vo.UserLoginInfo;
import com.ai.module.user.mapper.UserImageMapper;
import com.ai.module.user.service.ChatGptImageService;
import com.ai.module.chat.util.MrHeOpenAiService;
import com.ai.module.chat.util.aliyun.AliyunUtil;
import com.ai.module.mj.domain.req.SubmitImagineReq;
import com.ai.module.mj.domain.vo.SubmitResultVO;
import com.alibaba.fastjson2.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.time.LocalDateTime;

/**
 * @author: MrHe
 * @create: 2024-01-06 17:45
 * @description:
 **/
@Slf4j
@Service
public class ChatGptImageServiceImpl implements ChatGptImageService {

    @Autowired
    private AliyunProperty aliyunProperty;

    @Autowired
    UserImageMapper userImageMapper;

    @Override
    public SubmitResultVO createImage(SubmitImagineReq imageReq) {
        AliFileVo aliFileVo = downloadImage(imageReq);
        SubmitResultVO vo = new SubmitResultVO(aliFileVo.getFilePath(),aliFileVo.getScaleFilePath());
        return vo;
    }


    @Deprecated
    public AliFileVo downloadImage(SubmitImagineReq imageReq) {
        JSONObject image4 = MrHeOpenAiService.createImage4(imageReq.getPrompt());
        // assert jsonObject != null;
        // List<LinkedHashMap> mapList = jsonObject.getJSONArray("data").toJavaList(LinkedHashMap .class);
        // LinkedHashMap map= mapList.get(0);
        // return (String)map.get("url");
        UserLoginInfo userLoginInfo = ThreadLocalContext.getUserLoginInfo();
        return dealImageUrl("",userLoginInfo,imageReq) ;

    }

    //@Async("dealImageTask")
    public AliFileVo dealImageUrl(String url, UserLoginInfo userLoginInfo, SubmitImagineReq imagineReq) {
        String uuid = imagineReq.getUuid();
        try {
            String robotId = imagineReq.getRobotId();
            String projectName = userLoginInfo.getTaskImageUrl(FileStringConstant.ROBOT_IMAGE, robotId);
            AliFileVo aliFileVo = AliyunUtil.uploadImageAndGetUrl(url, projectName);
            asyncSaveImage(userLoginInfo.getUuid(),aliFileVo, projectName,uuid);
            return aliFileVo;
        }catch (Exception ex){
            ex.printStackTrace();
            log.error("文件文件处理异常[{}]", ex.getMessage());
            return new AliFileVo();
        }

    }



    @Async("dealImageTask")
    public void asyncSaveImage(String userUUidId,
                               AliFileVo aliFileVo,
                               String fileName,
                               String uuid) throws IOException {
        long currentTime = System.currentTimeMillis();
        UserImageEntity userImage = new UserImageEntity();
        userImage.setFilePath(aliFileVo.getFilePath());
        userImage.setUserId(userUUidId);
        userImage.setUserId(uuid);
        userImage.setExpireTime(aliFileVo.getExpireTime());
        userImage.setFileName(fileName);
        userImage.setScalePath(aliFileVo.getScaleFilePath());
        userImage.setBucketName(aliyunProperty.getOssBucketName());
        var projectName = aliFileVo.getProjectName();
        userImage.setProjectName(projectName);
        userImageMapper.insert(userImage);
        log.info("图片的真实链接为: " + aliFileVo.getFilePath());
    }

}
